/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.business
 * File:    ReportMgr
 * Created: Dec 25, 2010
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.daycare.business;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.made2ordersoftware.daycare.business.report.pdf.AgePDFReport;
import com.made2ordersoftware.daycare.business.report.pdf.ChildPDFReport;
import com.made2ordersoftware.daycare.business.report.pdf.DatePDFReport;
import com.made2ordersoftware.daycare.business.report.pdf.ParentPDFReport;
import com.made2ordersoftware.daycare.business.report.ReportBuilder;
import com.made2ordersoftware.daycare.domain.TimeWatchedEntity;
import com.made2ordersoftware.daycare.service.TimeWatchedDao;
import com.made2ordersoftware.daycare.service.exceptions.IncorrectReportTypeException;
import com.made2ordersoftware.exceptions.DAOException;
import java.io.FileNotFoundException;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Manages all activities preformed for reporting generation.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Stateless
public class ReportMgr {

    private static final Logger LOG = LoggerFactory.getLogger(ReportMgr.class);
    @EJB
    private TimeWatchedDao dao;
    @Inject
    private ReportBuilder builder;

    /**
     *
     * @param startDate
     * @param stopDate
     *
     * @return
     *
     * @throws DAOException
     * @throws IncorrectReportTypeException
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public Document generateDateReport(Timestamp startDate, Timestamp stopDate) throws
            DAOException, IncorrectReportTypeException, DocumentException, FileNotFoundException {
        LOG.info("Start generateDateReport from {} - {}", startDate, stopDate);

        List<TimeWatchedEntity> results = dao.getRecordsByDate(startDate, stopDate);

        LOG.info("Finish generateDateReport...");

        builder = new ReportBuilder(new DatePDFReport());

        return builder.builPdfdReport(results);

    }

    /**
     * Method which returns a list of TimeWatched records for time period and sorted by date.
     *
     * @param startDate Start date for report.
     * @param stopDate  Stop date for report.
     *
     * @return List of TimeWcthed records
     *
     * @throws DAOException
     * @throws IncorrectReportTypeException
     * @throws com.itextpdf.text.DocumentException
     * @throws java.io.FileNotFoundException
     */
    public Document generateAgeReport(Timestamp startDate, Timestamp stopDate) throws
            DAOException, IncorrectReportTypeException, DocumentException, FileNotFoundException {
        LOG.info("Start generateAgeReport from {} - {}", startDate, stopDate);

        List<TimeWatchedEntity> results = dao.getRecordsByAge(startDate, stopDate);

        LOG.info("Finish generateAgeReport...");

        builder = new ReportBuilder(new AgePDFReport());
        return builder.builPdfdReport(results);
    }

    /**
     *
     * @param startDate
     * @param stopDate
     * @param parentId
     *
     * @return
     *
     * @throws DAOException
     * @throws IncorrectReportTypeException
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public Document generateParentReport(Timestamp startDate, Timestamp stopDate, long parentId) throws
            DAOException, IncorrectReportTypeException, DocumentException, FileNotFoundException {
        LOG.info("Start generateParentReport for parent {}", parentId);

        List<TimeWatchedEntity> results = dao.getRecordsByParent(startDate, stopDate, parentId);

        LOG.info("Finish generateParentReport...");

        builder = new ReportBuilder(new ParentPDFReport());
        return builder.builPdfdReport(results);
    }

    /**
     *
     * @param startDate
     * @param stopDate
     * @param childId
     *
     * @return
     *
     * @throws DAOException
     * @throws IncorrectReportTypeException
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public Document generateChildReport(Timestamp startDate, Timestamp stopDate, long childId) throws
            DAOException, IncorrectReportTypeException, DocumentException, FileNotFoundException {
        LOG.info("Start generateParentReport for child {}", childId);

        List<TimeWatchedEntity> results = dao.getRecordsByChild(startDate, stopDate, childId);

        LOG.info("Finish generateChildReport...");

        builder = new ReportBuilder(new ChildPDFReport());
        return builder.builPdfdReport(results);
    }
}
